package com.demo.controller

import com.demo.db.mongodb.service.MongoDBService
import com.demo.db.mybatis.db1.service.MyBatisDB1Service
import com.demo.db.mybatis.db2.service.MyBatisDB2Service
import com.demo.db.redis.RedisService
import com.demo.model.Model_API
import com.yfree.global.YController
import com.yfree.socket.YSocketManager
import io.swagger.annotations.Api
import io.swagger.annotations.ApiImplicitParam
import io.swagger.annotations.ApiImplicitParams
import io.swagger.annotations.ApiOperation
import org.springframework.beans.factory.annotation.Value
import org.springframework.transaction.annotation.Transactional
import org.springframework.web.bind.annotation.RequestMapping
import org.springframework.web.bind.annotation.RequestMethod
import org.springframework.web.bind.annotation.RestController

@Api("ApiDoc")
@RestController
@RequestMapping("/api")
class ApiController(
        val myBatisDB1Service: MyBatisDB1Service,
        val myBatisDB2Service: MyBatisDB2Service,
        val redisService: RedisService,
        val mongoDBService: MongoDBService
) : YController() {
    @Value("\${demo.config1}")
    lateinit var config1: String

    @Value("\${demo.config2}")
    lateinit var config2: String

    @ApiOperation("index")
    @RequestMapping("/index", method = [RequestMethod.GET, RequestMethod.POST])
    fun index(): Any {
        return json()
    }

    @ApiOperation("sbin/interface")
    @ApiImplicitParams(
            ApiImplicitParam(name = "jkid", value = "接口ID", required = true, dataType = "String"),
            ApiImplicitParam(name = "jkparam", value = "接口参数", required = false, dataType = "String")
    )
    @Transactional
    @RequestMapping("/sbin", "/interface", method = [RequestMethod.GET, RequestMethod.POST])
    fun `interface`(jkid: String, jkparam: String?): Any {
        val params = params(jkparam, Map::class.java)
        val apiModel = Model_API()
        return when (jkid) {
            "00A00" -> { // 模拟出参
                apiModel.key = "value"
                json(result = arrayListOf(apiModel))
            }
            "00A01" -> { // 发送socket
                val param_socketID = params["socketID"].toString()
                val param_message = params["message"].toString()
                val socketIOClient = YSocketManager.socketClients[param_socketID]
                if (socketIOClient == null)
                    json(0, message = "socketID不存在")
                else {
                    socketIOClient.sendEvent("server", param_message)
                    json()
                }
            }
            "00A02" -> { // MySQL
                val param_id = params["id"].toString()
                json(result = myBatisDB1Service.find(param_id) + myBatisDB2Service.find(param_id))
            }
            "00A03" -> { // MongoDB
                val param_id = params["id"].toString()
                json(result = arrayListOf(mongoDBService.find(param_id)))
            }
            "00A04" -> { // Redis
                val param_id = params["id"].toString()
                redisService.saveID(param_id)
                apiModel.redis = redisService.readID()
                json(result = arrayListOf(apiModel))
            }
            "00A05" -> { // 获取自定义配置
                apiModel.config1 = config1
                apiModel.config2 = config2
                json(result = arrayListOf(apiModel))
            }
            else -> json(0)
        }
    }
}
